<?php

namespace App\Http\Controllers\platform;

use App\Http\Controllers\Controller;
use App\Models\Personal;
use App\Models\StudyUserTime;
use Illuminate\Http\Request;

class UserController extends Controller
{
    public function login(Request $Request)
    {
        if(!$Request->mobile){
            return response()->json(['code' => '403', 'msg' => '请输入用户名']);
        }
        if(!$Request->password){
            return response()->json(['code' => '403', 'msg' => '请输入密码']);
        }
        // 
        $data = Personal::select('id','name','head_img','tell','es_id')->where('tell',$Request->mobile)->first();
        if(!$data){
            return response()->json(['code' => '404', 'msg' => '账号不存在']);
        }
        if(!password_verify($Request->password, $data->passwd)){
            return response()->json(['code' => '403', 'msg' => '密码错误']);
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }
    
    public function studyTime(Request $Request)
    {
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $start_time = $Request->start_time;
        $end_time = $Request->end_time;
        $data = StudyUserTime::with('user')->selectRaw('uid, SUM(study_length) as study_length')
        ->where('eid',$Request->eid)->groupBy('uid')->orderBy('study_length','desc')
        ->when($start_time, function ($query, $start_time) {
            return $query->where(function($query) use ($start_time) {
                $query->where('create_time','>', $start_time);
            });
        })
        ->when($end_time, function ($query, $end_time) {
            return $query->where(function($query) use ($end_time) {
                $query->where('create_time','<', $end_time);
            });
        })->get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

}
